package com.ebt.m.proposal_v2.ui;

import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.ebt.m.proposal_v2.helper.NativeAPI;
import com.ebt.m.proposal_v2.ui.function.JsLoadSuccessListener;

/**
 * 计划书模块JS调用API类
 *
 * @author archer.qi
 *         Created on 2017/6/2.
 */
public class ProposalNativeAPI extends NativeAPI {

    /*Js页面加载完毕回调*/
    private JsLoadSuccessListener mJsLoadSuccessListener;

    private int id; // 标识API的唯一性
    private String url; // 绑定的url

    public ProposalNativeAPI(Context context) {
        super(context);
    }

    public ProposalNativeAPI(Context context, JsLoadSuccessListener listener) {
        super(context);
        mJsLoadSuccessListener = listener;
    }

    public ProposalNativeAPI setTag(int id) {
        this.id = id;
        return this;
    }

    public ProposalNativeAPI setUrl(String url) {
        this.url = url;
        return this;
    }

    @JavascriptInterface
    public void ebtAlert(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * Native请求Js方法后，Js方法通过调用该方法返回结果
     *
     * @param methodName
     * @param response
     */
    @JavascriptInterface
    public void callResponse(String methodName, String response) {
        if (mJSResponse == null) {
            mJSResponse = new JSResponse();
        }
        mJSResponse.methodName = methodName;
        mJSResponse.response = response;
    }

    /**
     * 产品计算器加载完毕
     */
    @JavascriptInterface
    public void loadSuccess(String message) {
        if (mJsLoadSuccessListener != null) {
            mJsLoadSuccessListener.onJsLoadSuccess(id, url);
        }
    }

}
